package leetcode.n2021;

import java.util.HashMap;

/**
 * https://leetcode-cn.com/problems/roman-to-integer/
 * @author Chaos
 * @date 2021/5/21
 * 字符          数值
 * I             1
 * V             5
 * X             10
 * L             50
 * C             100
 * D             500
 * M             1000
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/roman-to-integer
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class RomanToInteger {

    public static void main(String[] args) {
        RomanToInteger t = new RomanToInteger();
        System.out.println(t.test("MCMXCIV"));
    }


    public int test(String s) {
        int result = 0;

        HashMap<String, Integer> map = new HashMap<>(8);
        map.put("I",1);
        map.put("V",5);
        map.put("X",10);
        map.put("L",50);
        map.put("C",100);
        map.put("D",500);
        map.put("M",1000);

        for (int i = 0; i < s.length(); i++) {
            int c1 = map.get(String.valueOf(s.toCharArray()[i]));
            int c2 = 0;
            if (i < s.length()-1) {
                i++;
                c2 = map.get(String.valueOf(s.toCharArray()[i]));
                if (c2 > c1) {
                    c1 = c2 - c1;
                } else {
                    i--;
                }
                c2 = 0;
            }

            result += c1 + c2;
        }

        return result;
    }
}
